﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using bdxyit.com.RecruitmentAppSys.ViewModels;
using System.Diagnostics;
using bdxyit.com.RecruitmentAppSys.Models;
using bdxyit.com.RecruitmentAppSys.BLL;

namespace bdxyit.com.RecruitmentAppSys.Controllers
{
    public class CandidatePositionsController : Controller
    {
        // GET: /CandidatePositions/
        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /CandidatePositions/Details/5

        public ActionResult Details(string id)
        {
            CandidatePositionsViewModels cpvm = CandidatePositionsViewModelBLL.DetailsCandidatePositions(id);
            return View(cpvm);
        }

        //  
        // GET: /CandidatePositions/Create

        public ActionResult Create(string id)
        {
            CandidatePositionsViewModels cpvm = CandidatePositionsViewModelBLL.InitCandidatePositionsView(id);
            return View(cpvm);
        }


        //
        // POST: /CandidatePositions/Create

        [HttpPost]
        public ActionResult Create(CandidatePositionsViewModels CandidatePositions, FormCollection form)
        {
            string id = form["UserInfo.UserID"].ToString();
            string[] post = form["PostID"].Split(new char[] { ',' });
            string[] Company = form["CompanyID"].Split(new char[] { ',' });
            CandidatePositions.ApplyJobActionFirst = new ApplyJobAction();
            CandidatePositions.ApplyJobActionSecond = new ApplyJobAction();
            CandidatePositions.ApplyJobActionThird = new ApplyJobAction();

            try
            {
                ApplyJobAction cpf1 = ApplyJobActionBLL.GetApplyJobActionByUserIdAndFlag(id, 1);
                if (cpf1 != null)
                {
                    ApplyJobActionBLL.RemoveApplyJobActionByUserIdAndFlag(id, 1);
                    CandidatePositions.ApplyJobActionFirst.ID = Guid.NewGuid().ToString();
                    CandidatePositions.ApplyJobActionFirst.UserID = id;
                    CandidatePositions.ApplyJobActionFirst.PostID = post[0].ToString();
                    CandidatePositions.ApplyJobActionFirst.CompanyID = Company[0].ToString();
                    CandidatePositions.ApplyJobActionFirst.ActionID = 1;
                    CandidatePositions.ApplyJobActionFirst.Flag = 1;
                    ApplyJobActionBLL.CreateApplyJobAction(CandidatePositions.ApplyJobActionFirst);
                }
                ApplyJobAction cpf2 = ApplyJobActionBLL.GetApplyJobActionByUserIdAndFlag(id, 2);
                if (cpf2 != null)
                {
                    ApplyJobActionBLL.RemoveApplyJobActionByUserIdAndFlag(id, 2);
                    CandidatePositions.ApplyJobActionSecond.ID = Guid.NewGuid().ToString();
                    CandidatePositions.ApplyJobActionSecond.UserID = id;
                    CandidatePositions.ApplyJobActionSecond.PostID = post[1].ToString();
                    CandidatePositions.ApplyJobActionSecond.CompanyID = Company[1].ToString();
                    CandidatePositions.ApplyJobActionSecond.ActionID = 1;
                    CandidatePositions.ApplyJobActionSecond.Flag = 2;
                    ApplyJobActionBLL.CreateApplyJobAction(CandidatePositions.ApplyJobActionSecond);
                }
                ApplyJobAction cpf3 = ApplyJobActionBLL.GetApplyJobActionByUserIdAndFlag(id, 3);
                if (cpf3 != null)
                {
                    ApplyJobActionBLL.RemoveApplyJobActionByUserIdAndFlag(id, 3);
                    CandidatePositions.ApplyJobActionThird.ID = Guid.NewGuid().ToString();
                    CandidatePositions.ApplyJobActionThird.UserID = id;
                    CandidatePositions.ApplyJobActionThird.PostID = post[2].ToString();
                    CandidatePositions.ApplyJobActionThird.CompanyID = Company[2].ToString();
                    CandidatePositions.ApplyJobActionThird.ActionID = 1;
                    CandidatePositions.ApplyJobActionThird.Flag = 3;
                    ApplyJobActionBLL.CreateApplyJobAction(CandidatePositions.ApplyJobActionThird);
                }
                var usInfo = UserInfoBLL.GetBasicInfoByUserID(id);
                if (usInfo != null)
                {
                    usInfo.Other = CandidatePositions.UserInfo.Other;
                    UserInfoBLL.EditBasicInfo(usInfo);
                }
                return RedirectToAction("Create", "UserInfo", new
                {
                    ID = id
                });
            }
            catch
            {
                return RedirectToAction("Edit");
            }
        }

        //
        // GET: /CandidatePositions/Edit/5

        public ActionResult Edit(string id)
        {
            CandidatePositionsViewModels cpvm = CandidatePositionsViewModelBLL.EditCandidatePositions(id);
            return View(cpvm);
        }

        //
        // POST: /CandidatePositions/Edit/5
        [HttpPost]
        public ActionResult Edit(CandidatePositionsViewModels CandidatePositions, FormCollection form)
        {
            string id = form["UserInfo.UserID"].ToString();
            string[] post = form["PostID"].Split(new char[] { ',' });
            string[] Company = form["CompanyID"].Split(new char[] { ',' });
            CandidatePositions.ApplyJobActionFirst = new ApplyJobAction();
            CandidatePositions.ApplyJobActionSecond = new ApplyJobAction();
            CandidatePositions.ApplyJobActionThird = new ApplyJobAction();

            try
            {
                ApplyJobAction cpf1 = ApplyJobActionBLL.GetApplyJobActionByUserIdAndFlag(id, 1);
                if (cpf1 != null)
                {
                    ApplyJobActionBLL.RemoveApplyJobActionByUserIdAndFlag(id, 1);
                    CandidatePositions.ApplyJobActionFirst.ID = Guid.NewGuid().ToString();
                    CandidatePositions.ApplyJobActionFirst.UserID = id;
                    CandidatePositions.ApplyJobActionFirst.PostID = post[0].ToString();
                    CandidatePositions.ApplyJobActionFirst.CompanyID = Company[0].ToString();
                    CandidatePositions.ApplyJobActionFirst.ActionID = 1;
                    CandidatePositions.ApplyJobActionFirst.Flag = 1;
                    ApplyJobActionBLL.CreateApplyJobAction(CandidatePositions.ApplyJobActionFirst);
                }
                ApplyJobAction cpf2 = ApplyJobActionBLL.GetApplyJobActionByUserIdAndFlag(id, 2);
                if (cpf2 != null)
                {
                    ApplyJobActionBLL.RemoveApplyJobActionByUserIdAndFlag(id, 2);
                    CandidatePositions.ApplyJobActionSecond.ID = Guid.NewGuid().ToString();
                    CandidatePositions.ApplyJobActionSecond.UserID = id;
                    CandidatePositions.ApplyJobActionSecond.PostID = post[1].ToString();
                    CandidatePositions.ApplyJobActionSecond.CompanyID = Company[1].ToString();
                    CandidatePositions.ApplyJobActionSecond.ActionID = 1;
                    CandidatePositions.ApplyJobActionSecond.Flag = 2;
                    ApplyJobActionBLL.CreateApplyJobAction(CandidatePositions.ApplyJobActionSecond);
                }
                ApplyJobAction cpf3 = ApplyJobActionBLL.GetApplyJobActionByUserIdAndFlag(id, 3);
                if (cpf3 != null)
                {
                    ApplyJobActionBLL.RemoveApplyJobActionByUserIdAndFlag(id, 3);
                    CandidatePositions.ApplyJobActionThird.ID = Guid.NewGuid().ToString();
                    CandidatePositions.ApplyJobActionThird.UserID = id;
                    CandidatePositions.ApplyJobActionThird.PostID = post[2].ToString();
                    CandidatePositions.ApplyJobActionThird.CompanyID = Company[2].ToString();
                    CandidatePositions.ApplyJobActionThird.ActionID = 1;
                    CandidatePositions.ApplyJobActionThird.Flag = 3;
                    ApplyJobActionBLL.CreateApplyJobAction(CandidatePositions.ApplyJobActionThird);
                }
                var usInfo = UserInfoBLL.GetBasicInfoByUserID(id);
                if (usInfo != null)
                {
                    usInfo.Other = CandidatePositions.UserInfo.Other;
                    UserInfoBLL.EditBasicInfo(usInfo);
                }
                return RedirectToAction("Create", "UserInfo", new
                {
                    ID = id
                });
            }
            catch
            {
                return RedirectToAction("Edit");
            }
        }

        //
        // GET: /CandidatePositions/Delete/5

        public ActionResult Delete(string id)
        {
            try
            {
                bool flag = ApplyJobActionBLL.RemoveApplyJobActionByUserId(id);
                return RedirectToAction("Details", "FamilyInfo", new
                {
                    ID = id
                });
            }
            catch
            {
                return RedirectToAction("Index");
            }
        }
    }
}
